package com.dgut.cbd.finalexp.service;

import com.dgut.cbd.finalexp.dao.UserDao;
import com.dgut.cbd.finalexp.entity.MyUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

/**
 * @Program: finalexp
 * @Description:
 * @Author: cbd
 * @Create: 2021-06-21
 * @Version: 1.0.0
 **/
@Service
public class UserService implements UserDetailsService {

    @Autowired
    UserDao userDao;

    @Override
    public UserDetails loadUserByUsername(String id) throws UsernameNotFoundException {
        System.out.println("请求认证的用户id：" + id);

        MyUser user = userDao.loadUserFromId(id);

        if(user==null){
            throw new UsernameNotFoundException("用户名或密码错误");
        }

        return new User(id, user.getPassword(), AuthorityUtils.commaSeparatedStringToAuthorityList("USER"));
    }


}
